import type { Msg, NewMessageListener } from "@/components/messages/message-types";

const LISTENER: NewMessageListener[] = []

export function addNewMessageLister(listener: NewMessageListener){
    LISTENER.push(listener)
}

export function newMessage(msg: Msg){
  // todo 这里应该定义一个ID ,方便手动关闭等待
  LISTENER.forEach(value => {
    try {
      value({
        msg
      })
    } catch (e){
      console.error(e)
    }
  })
}

export function error(message: string){
  newMessage({text: message, timeout: 10000, type: 'error'})
}

export function success(message: string){
  newMessage({text: message, timeout: 10000, type: 'success'})
}
